/*
 * Copyright 2010 Chad Retz
 * 
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not
 * use this file except in compliance with the License. You may obtain a copy of
 * the License at
 * 
 * http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
 * License for the specific language governing permissions and limitations under
 * the License.
 */
package org.jdocng.shared.model;

import javax.xml.bind.annotation.XmlType;

import com.google.common.annotations.GwtCompatible;
import com.google.gson.annotations.Expose;
import com.sun.javadoc.PackageDoc;
import com.sun.javadoc.SeeTag;

/**
 * Implementation of {@link SeeTag}
 *
 * @author Chad Retz
 */
@XmlType(name = "seeTag")
@GwtCompatible(serializable = true)
@SuppressWarnings("serial")
public class SeeTagImpl extends TagImpl implements SeeTag {

    //exposed fields
    
    @Expose
    private String label;
    
    @Expose
    private String referencedClass;
    
    @Expose
    private String referencedMember;
    
    @Expose
    private String referencedPackage;
    
    //non-exposed fields
    
    private transient ClassDocImpl referencedClassLoaded;
    
    private transient MemberDocImpl referencedMemberLoaded;
    
    private transient PackageDocImpl referencedPackageLoaded;
    
    //SeeTag interface impl
    
    @Override
    public String label() {
        return label;
    }
    
    @Override
    public ClassDocImpl referencedClass() {
        if (referencedClass == null) {
            return null;
        }
        if (referencedClassLoaded == null) {
            referencedClassLoaded = getContext().getClassDoc(referencedClass);
        }
        return referencedClassLoaded;
    }
    
    @Override
    public String referencedClassName() {
        return referencedClass;
    }
    
    @Override
    public MemberDocImpl referencedMember() {
        if (referencedClass == null) {
            return null;
        }
        if (referencedMemberLoaded == null) {
            //TODO: be smarter about checks instead of the ambiguous name!
            //fields first
            for (FieldDocImpl field : referencedClass().fields()) {
                if (referencedMember.equals(field.name())) {
                    referencedMemberLoaded = field;
                    break;
                }
            }
            //TODO: gotta do the name thing, constructors suck w/out it
        }
        return referencedMemberLoaded;
    }
    
    @Override
    public String referencedMemberName() {
        return referencedMember;
    }
    
    @Override
    public PackageDoc referencedPackage() {
        if (referencedPackage == null) {
            return null;
        }
        if (referencedPackageLoaded == null) {
            referencedPackageLoaded = getContext().getPackageDoc(referencedPackage);
        }
        return referencedPackageLoaded;
    }
    
    //other accessors
    
    public void setLabel(String label) {
        this.label = label;
    }
    
    public void setReferencedClass(String referencedClass) {
        this.referencedClass = referencedClass;
    }
    
    public void setReferencedMember(String referencedMember) {
        this.referencedMember = referencedMember;
    }
    
    public String getReferencedPackage() {
        return referencedPackage;
    }
    
    public void setReferencedPackage(String referencedPackage) {
        this.referencedPackage = referencedPackage;
    }
}
